import { SysDept } from '../../depts/entities/dept.entity';
import { SysRole } from '../../roles/entities/role.entity';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  Index,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Index('login_name', ['username'], { unique: true })
@Index('dept_id', ['deptId'], {})
@Entity('sys_user', { schema: 'lzo_admin_nestjs' })
export class SysUser {
  @PrimaryGeneratedColumn({ type: 'bigint', name: 'id' })
  id: string;

  @Column('varchar', {
    name: 'username',
    nullable: true,
    unique: true,
    comment: '用户名',
    length: 64,
  })
  username: string | null;

  @Column('varchar', {
    name: 'nickname',
    nullable: true,
    comment: '昵称',
    length: 64,
  })
  nickname: string | null;

  @Column('int', {
    name: 'gender',
    nullable: true,
    comment: '性别((1-男 2-女 0-保密)',
    default: () => "'1'",
  })
  gender: number | null;

  @Column('varchar', {
    name: 'password',
    nullable: true,
    comment: '密码',
    length: 100,
  })
  password: string;

  @Column('varchar', {
    name: 'avatar',
    nullable: true,
    comment: '用户头像',
    length: 255,
  })
  avatar: string | null;

  @Column('varchar', {
    name: 'mobile',
    nullable: true,
    comment: '联系方式',
    length: 20,
  })
  mobile: string | null;

  @Column('int', {
    name: 'status',
    nullable: true,
    comment: '状态(1-正常 0-禁用)',
    default: () => "'1'",
  })
  status: number | null;

  @Column('varchar', {
    name: 'email',
    nullable: true,
    comment: '用户邮箱',
    length: 128,
  })
  email: string | null;

  @CreateDateColumn({
    name: 'create_time',
    nullable: true,
    comment: '创建时间',
  })
  createTime: Date | string | null;

  @Column('bigint', { name: 'create_by', nullable: true, comment: '创建人ID' })
  createBy: string | null;

  @UpdateDateColumn({
    name: 'update_time',
    nullable: true,
    comment: '更新时间',
  })
  updateTime: Date | string | null;

  @Column('bigint', { name: 'update_by', nullable: true, comment: '修改人ID' })
  updateBy: string | null;

  @DeleteDateColumn({
    name: 'is_deleted',
    nullable: true,
    comment: '逻辑删除标识(0/null-未删除 1-已删除)',
    default: () => null,
  })
  isDeleted: number | null;

  @Column('char', {
    name: 'openid',
    nullable: true,
    comment: '微信 openid',
    length: 28,
  })
  openid: string | null;

  @Column('datetime', { name: 'deleted_at', nullable: true })
  deletedAt: Date | null;

  @Column('bigint', { name: 'dept_id', nullable: true })
  deptId: string | null;

  @ManyToOne(() => SysDept, (sysDept) => sysDept.sysUsers, {
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'dept_id', referencedColumnName: 'id' }])
  dept: SysDept;

  @ManyToMany(() => SysRole, (sysRole) => sysRole.sysUsers)
  @JoinTable({
    name: 'sys_user_role',
    joinColumns: [{ name: 'user_id', referencedColumnName: 'id' }],
    inverseJoinColumns: [{ name: 'role_id', referencedColumnName: 'id' }],
    schema: 'lzo_admin_nestjs',
  })
  sysRoles: SysRole[];

  [key: string]: any; // 防止类型检查报错
}
